package com.AdvancedDemos.集合.单列集合;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class 电影信息集合遍历Demo {
    public static void main(String[] args) {
        //1.创建一个集合容器来存储多个电影对象
        Collection<Movie> movies = new ArrayList<>();//注意ArrayList中每个元素都是Movie类型变量,里面只存着指向的Movie对象的地址
        movies.add(new Movie("<肖申克的救赎>", 9.7, "罗宾斯"));
        movies.add(new Movie("<The Walling Dead>", 10.0, "达里尔,卡罗尔,米琼恩,茱蒂丝,尼根,卡尔,瑞克"));
        movies.add(new Movie("<霸王别姬>", 9.6, "项羽,虞姬"));
        movies.add(new Movie("<阿甘正传>", 9.5, "汤姆.汉克斯"));
        System.out.println(movies);


        //2.遍历集合获取电影信息
        System.out.println("--------------------");
        System.out.println("1使用迭代器iterator");
        Iterator<Movie> iterator = movies.iterator();
        while (iterator.hasNext()) {
            Movie ele = iterator.next();
            System.out.println("--------------------");
            System.out.println("电影名称 : " + ele.getName());
            System.out.println("电影评分 : " + ele.getScore());
            System.out.println("演员 : " + ele.getActor());
        }
        System.out.println("--------------------");
        System.out.println("2使用增强for循环");
        for (Movie ele : movies) {
            System.out.println("--------------------");
            System.out.println("电影名称 : " + ele.getName());
            System.out.println("电影评分 : " + ele.getScore());
            System.out.println("演员 : " + ele.getActor());
        }
        System.out.println("--------------------");
        System.out.println("3使用forEach方法");
        movies.forEach(ele -> {
            System.out.println("--------------------");
            System.out.println("电影名称 : " + ele.getName());
            System.out.println("电影评分 : " + ele.getScore());
            System.out.println("演员 : " + ele.getActor());
        });
        System.out.println("--------------------");

    }
}

class Movie {
    private String name;
    private double score;
    private String actor;

    public Movie() {
    }

    public Movie(String name, double score, String actor) {
        this.name = name;
        this.score = score;
        this.actor = actor;
    }

    @Override
    public String toString() {
        return "Movie{" +
                "name='" + name + '\'' +
                ", score=" + score +
                ", actor='" + actor + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public String getActor() {
        return actor;
    }

    public void setActor(String actor) {
        this.actor = actor;
    }
}
